<?php
$_options = array(
	'data'			=>	array(),
	'expand_all'	=>	true,
	'attributes'	=>	array(),
	'fields'		=>	array(
		'text'	=>	'name',
		'id'	=>	'id'
	),
	'model'	=>	'Category',
	'class'	=>	""
);

// expand style

if (isset($settings)) {
	if (isset($settings['model'])) {
		$parts = explode(".", $settings['model']);
		if (count($parts) > 1) {
			$settings['model'] = $parts[1];
		} else {
			$settings['model'] = $parts[0];
		}
	}
	foreach ($settings as $key => $val) {
		$_options[$key] = $val;
	}
}

$_options['class'] = $_options['expand_all'] ? 'class="open"' : '';
?>

<?php

$attr_strs = array();
foreach ($_options['attributes'] as $key => $val) {
	$attr_strs[] = "{$key}=\"{$val}\"";
}


if (!function_exists('generatechildren')) {
	function generateChildren($nodes, &$_options) {
		$content = "<ul>\n";
		foreach($nodes as $item) {
			if (!(isset($item['items']) && count($item['items']))) {
				$class = '';
			} else {
				$class = $_options['class'];
			}
			
			$child_items = array();
			
			if (isset($item['items'])) {
				$child_items = $item['items'];
			}
			
			$item = $item[$_options['model']];
			
			$content .= "<li node_id='{$item[$_options['fields']['id']]}' id=\"phtml_{$item[$_options['fields']['id']]}\" {$class}><a href=\"#\"><ins>&nbsp;</ins>{$item[$_options['fields']['text']]}</a>\n";
			if (count($child_items)) {
				$content .= generateChildren($child_items, $_options);
			}
		}
		
		$content .= "</ul>\n";
		return $content;
	}
}
?>

<div <?php echo implode(" ", $attr_strs)?>>
	<?echo generateChildren($_options['data'], $_options);?>
</div>